import gzip
import os


def un_gz(file_name):
    """ungz zip file"""
    f_name = file_name.replace(".gz", "")
    # 获取文件的名称，去掉
    g_file = gzip.GzipFile(file_name)
    # 创建gzip对象
    open(f_name, "wb+").write(g_file.read())
    # gzip对象用read()打开后，写入open()建立的文件里。
    g_file.close() # 关闭gzip对象


if __name__ == '__main__':
    dir_path = "D:/download/主驾误唤醒-车机日志"
    for filepath, dirnames, filenames in os.walk(dir_path):
        for filename in filenames:
            if not filename.endswith(".gz"):
                continue
            un_gz_filename = filename.replace(".gz", "")
            if os.path.exists(os.path.join(filepath, un_gz_filename)):
                continue
            un_gz(os.path.join(filepath, filename))